/*
 * Copyright 2017 hv0912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vimhe.ezimp2;

import com.vimhe.ezimp2.config.ImportConfig;
import com.vimhe.ezimp2.exception.ImportException;
import com.vimhe.ezimp2.handler.DataHandler;
import com.vimhe.ezimp2.handler.Formatter;
import com.vimhe.ezimp2.handler.Validator;
import com.vimhe.ezimp2.handler.reader.Excel2003Handler;
import com.vimhe.ezimp2.handler.reader.Excel2007Handler;
import com.vimhe.ezimp2.util.FilenameUtils;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.xml.sax.SAXException;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * 数据导入管理器
 *
 * @author Vimhe
 */
public class ImportManager {

    /**
     * 文件索引
     */
    private String fileIndex;

    /**
     * 文件格式
     */
    private String excelFormat;

    /**
     * 文件流
     */
    private InputStream inputStream;

    /**
     * 配置管理对象
     */
    private ImportConfig importConfig;

    /**
     * 数据处理器
     */
    private DataHandler dataHandler;

    public ImportManager(String fileIndex, String excelFormat, InputStream inputStream,
                         ImportConfig importConfig, DataHandler dataHandler) {
        this.fileIndex = fileIndex;
        this.excelFormat = excelFormat;
        this.inputStream = inputStream;
        this.importConfig = importConfig;
        this.dataHandler = dataHandler;
    }

    public ImportManager(String fileIndex, String filePath,
                         ImportConfig importConfig, DataHandler dataHandler)
            throws FileNotFoundException {
        this.fileIndex = fileIndex;
        this.excelFormat = FilenameUtils.getExtension(filePath).toLowerCase();
        this.inputStream = new FileInputStream(filePath);
        this.importConfig = importConfig;
        this.dataHandler = dataHandler;
    }

    /**
     * 开始解析
     *
     * @throws IOException        e
     * @throws SAXException       e
     * @throws OpenXML4JException e
     */
    public void process() throws IOException, SAXException, OpenXML4JException {
        if (Constants.EXCEL_FORMAT_XLS.equals(excelFormat)) {
            new Excel2003Handler(dataHandler, importConfig).process(fileIndex, inputStream);
        } else if (Constants.EXCEL_FORMAT_XLSX.equals(excelFormat)) {
            new Excel2007Handler(dataHandler, importConfig).process(fileIndex, inputStream);
        } else {
            throw new ImportException("不支持的文件格式");
        }
    }

    /**
     * 设置数据验证器
     *
     * @param validator 数据验证器
     * @return this
     */
    public ImportManager addValidator(Validator validator) {
        this.dataHandler.addValidator(validator);
        return this;
    }

    /**
     * 设置数据格式化器
     *
     * @param formatter 数据格式化器
     */
    public ImportManager addFormatter(Formatter formatter) {
        this.dataHandler.addFormatter(formatter);
        return this;
    }

}
